package com.student.education.controller;

import com.student.education.common.CommonPage;
import com.student.education.common.CommonResult;
import com.student.education.entity.Exam;
import com.student.education.service.ExamService;
import com.student.education.to.ExamQueryTo;
import com.student.education.vo.ExamQueryVo;
import com.student.education.vo.ExamVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 考试类型维护表(Exam)表控制层
 *
 * @author makejava
 * @since 2021-10-10 00:23:54
 */
@RestController
@RequestMapping("exam")
public class ExamController {
    /**
     * 服务对象
     */
    @Resource
    private ExamService examService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Exam> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.examService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param exam 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody ExamVo exam) {
        if (examService.insert(exam)>0){
            return CommonResult.success("新增成功");
        }
        return CommonResult.success("新增失败");
    }

    /**
     * 新增数据
     *
     * @param
     * @return 新增结果
     */
    @PostMapping("/queryAll")
    public CommonResult<CommonPage<ExamQueryTo>> queryAll(@RequestBody ExamQueryVo vo) {
        return CommonResult.success(examService.queryAll(vo));
    }

    /**
     * 编辑数据
     *
     * @param exam 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public CommonResult<String> edit(@RequestBody ExamVo exam) {
        if (examService.update(exam)>0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.error("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public CommonResult<String> deleteById(Integer id) {

        if (examService.deleteById(id)){
            return CommonResult.success("删除成功");
        }
        return CommonResult.error("删除失败");
    }

}

